package com.kaiwukj.android.ufamily.mvp.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.irozon.sneaker.widget.RoundedImageView;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.DynamicResult;
import com.kaiwukj.android.ufamily.mvp.ui.widget.RecycleJZVideoPlayer;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jzvd.JZDataSource;
import cn.jzvd.JzvdStd;

/**
 * @author Charles
 * @date 2019/8/22.
 * description：Kaiwu
 */
public class RecycleVideoAdapter extends RecyclerView.Adapter {

    //CircleHomeResult

    private List<DynamicResult> datas = new ArrayList<>();

    private OnClickListener onClickListener;

    private Context context;

    public void setDatas(List<DynamicResult> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public void addDatas(List<DynamicResult> datas) {
        this.datas.addAll(datas);
        notifyDataSetChanged();
    }

    public RecycleVideoAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public RecycleVideoHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_recycle_video, parent, false);
        return new RecycleVideoHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        RecycleVideoHolder videoHolder = (RecycleVideoHolder) holder;

        Glide.with(context).load(datas.get(position).getImages()).into(videoHolder.video.posterImageView);
        JZDataSource jzDataSource = new JZDataSource(datas.get(position).getVideo());
        jzDataSource.looping = true;
        videoHolder.video.setUp(jzDataSource, JzvdStd.SCREEN_NORMAL);
        if (position == 0) {
            videoHolder.video.startVideo();
        }
        videoHolder.tvName.setText(datas.get(position).getUserName());
        videoHolder.tvContent.setText(datas.get(position).getDynamicContent());
        Glide.with(context).load(datas.get(position).getHeadImg()).into(videoHolder.rivHead);
        if (!datas.get(position).isPraise()) {
            videoHolder.ivLike.setSelected(false);
        } else {
            videoHolder.ivLike.setSelected(true);
        }
        videoHolder.tvLikeNum.setText(datas.get(position).getThumbNum() + "");
        videoHolder.tvCommentNum.setText(datas.get(position).getCommentNum() + "");

        ViewGroup.LayoutParams layoutParams = videoHolder.itemView.getLayoutParams();
        layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;

//        点击头像
        videoHolder.rivHead.setOnClickListener(view ->
                onClickListener.OnHeadListener(position));
//        评论
        videoHolder.llComment.setOnClickListener(view -> onClickListener.OnCommentListener(position));
//        icon_social_post_card
        videoHolder.llLike.setOnClickListener(view -> {
            datas.get(position).setThumbNum(datas.get(position).getThumbNum() + (datas.get(position).isPraise() ? -1 : 1));
            onClickListener.OnLikeListener(position, datas.get(position).isPraise());
            datas.get(position).setPraise(datas.get(position).isPraise() ? 0 : 1);
            videoHolder.tvLikeNum.setText(datas.get(position).getThumbNum() + "");
            videoHolder.ivLike.setSelected(!videoHolder.ivLike.isSelected());
        });
//        转发
        videoHolder.llTransmit.setOnClickListener(view ->
                onClickListener.OnTransmitListener(position));
//        关注
        videoHolder.ivFocus.setOnClickListener(view ->
                onClickListener.OnFocusListener(position));
//        发出评论
        videoHolder.tvSend.setOnClickListener(view -> {
            onClickListener.OnSendCommentListener(position, videoHolder.etEveluate.getText().toString());
            videoHolder.etEveluate.setText("");
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public class RecycleVideoHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.video)
        public RecycleJZVideoPlayer video;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_content)
        TextView tvContent;
        @BindView(R.id.et_eveluate)
        EditText etEveluate;
        @BindView(R.id.riv_head)
        RoundedImageView rivHead;
        @BindView(R.id.iv_focus)
        ImageView ivFocus;
        @BindView(R.id.ll_like)
        LinearLayout llLike;
        @BindView(R.id.ll_comment)
        LinearLayout llComment;
        @BindView(R.id.ll_transmit)
        LinearLayout llTransmit;
        @BindView(R.id.tv_like_num)
        TextView tvLikeNum;
        @BindView(R.id.tv_comment_num)
        public TextView tvCommentNum;
        @BindView(R.id.iv_like)
        ImageView ivLike;
        @BindView(R.id.tv_send)
        TextView tvSend;

        public RecycleVideoHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnClickListener {
        void OnLikeListener(int pos, boolean isLike);

        void OnCommentListener(int pos);

        void OnTransmitListener(int pos);

        void OnFocusListener(int pos);

        void OnSendCommentListener(int pos, String text);

        void OnHeadListener(int pos);
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }
}
